require("script-loader!file-saver")
import * as ExcelJs from "exceljs/dist/exceljs";


/**
 * 设置工作簿相关属性
 * @param {*} workbook 
 */
export function set_workbook() {
    // 创建一个工作簿
    const workbook = new ExcelJs.Workbook();
    workbook.creator = '管理员';
    workbook.lastModifiedBy = '管理员';
    workbook.created = new Date(1985, 8, 30);
    workbook.modified = new Date();
    workbook.lastPrinted = new Date(2016, 9, 27);

    // 在加载时强制工作簿计算属性
    workbook.calcProperties.fullCalcOnLoad = true;
    // 工作簿视图控制在查看工作簿时 Excel 将打开多少个单独的窗口。
    workbook.views = [
        {
            x: 0, y: 0, width: 10000, height: 20000,
            firstSheet: 0, activeTab: 1, visibility: 'visible'
        }
    ]
    return workbook
}

/**
 * 设置sheet表相关属性
 */
export function set_sheet(workbook, name) {
    const ws1 = workbook.addWorksheet(name);
    return ws1
}


/**
 * 添加列标题并 添加列标题并设置列属性
 */
export function set_columns_name(ws, header) {
    const columnsList = header.map((obj, idx) => {
        const exportArgument = obj?.exportArgument || {}
        const width = exportArgument?.width || 26
        let columnsObj = {
            header: obj.headerName,
            key: obj.field,
            width,
        }
        // //通过列来判断是什么类型
        const cellType = exportArgument?.type || "text"
        if (cellType == 'date') {
            columnsObj.style = { numFmt: 'yyyy-MM-dd' }
        }
        if (cellType == 'amount') {
            columnsObj.style = { numFmt: '#,##0.00;#,##0.00;' }
        }
        if (cellType == 'number') {
            columnsObj.style = { numFmt:'0.00' }
        }
        if (cellType == 'text') {
            columnsObj.style = { numFmt:'@' }
        }


        return columnsObj
    })
    ws.columns = columnsList
}


/**
 * 保存并生成excel
 * @param {*} workbook 
 * @param {*} filename 
 * @param {*} bookType 
 */
export function save_excel(workbook, filename, bookType) {
    // 保存设置
    workbook.xlsx.writeBuffer().then((buffer) => {
        // eslint-disable-next-line no-undef
        saveAs(
            new Blob([buffer], {
                type: "application/octet-stream",
            }),
            `${filename}.${bookType}`
        );
    });
}

/**
 * 获取某个单元格的的位置
 * 例如：A1 B1 A2 B2
 * @param colKey 第几列
 * @param rowKey 第几行
 */
export function getColmusKey(colKey, rowKey) {
    for (let i = 0; i < 26; i++) {
        // console.log(String.fromCharCode(65+i));//输出A-Z 26个大写字母}
        if (colKey == i) {
            return `${String.fromCharCode(65 + colKey)}${rowKey}`
        }
    }
}

/**
 * 封装rgbaTo16color功能函数
 * 功能: 把rgba颜色(或rgb颜色)转成十六进制颜色
 */
export function rgbaTo16color(color) {
    let val = color
      .replace(/rgba?\(/, '')
      .replace(/\)/, '')
      .replace(/[\s+]/g, '')
      .split(',');
    let a = parseFloat(val[3] || 1),
        r = Math.floor(a * parseInt(val[0]) + (1 - a) * 255),
        g = Math.floor(a * parseInt(val[1]) + (1 - a) * 255),
        b = Math.floor(a * parseInt(val[2]) + (1 - a) * 255);
    return "#" +
      ("0" + r.toString(16)).slice(-2) +
      ("0" + g.toString(16)).slice(-2) +
      ("0" + b.toString(16)).slice(-2);
  }

  /**
 * 封装hexToRGBA功能函数
 * 功能: 把十六进制颜色转成rgba颜色(或rgb颜色)
 */
export function hexToRGBA(hex, alpha) {
    let r = parseInt(hex.slice(1, 3), 16);
    let g = parseInt(hex.slice(3, 5), 16);
    let b = parseInt(hex.slice(5, 7), 16);
    if (alpha) {
      return "rgba(" + r + ", " + g + ", " + b + ", " + alpha + ")";
    } else {
      return "rgb(" + r + ", " + g + ", " + b + ")";
    }
  }


